package io.github.biezhi.elves.service.impl;

import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.github.biezhi.elves.utils.HttpUtil;
import io.github.biezhi.elves.utils.StringUtils;
import io.github.biezhi.elves.model.EbaseInfo;
import io.github.biezhi.elves.mapper.ShareholderMapper;
import io.github.biezhi.elves.service.StockStructureService;
import io.github.biezhi.elves.model.StockStructureProfileInfo;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *	股权结构信息业务接口实现类 
 *
 */
public class StockStructureServiceImpl implements StockStructureService {
	@Autowired
	private ShareholderMapper shareholderMapper;
	
	/**
	 * 解析json数据并保存股权结构图数据入库
	 * @param cmsAsynUrl
	 * @param jsonStr
	 * @param unique
	 * @param entname
	 * @param requestEntnameVal
	 */
	public void saveStockStructureProfileData(String cmsAsynUrl,String jsonStr,String unique,String entname,String requestEntnameVal){
		String resultJson = "";
		try {
			JSONObject json = JSONObject.parseObject(jsonStr);
			if(json.containsKey("Result")){
				resultJson = json.getString("Result");
			}else{
				resultJson = json.getString("Children");
			}
			JSONObject resultJsonObject = JSONObject.parseObject(resultJson);
			StockStructureProfileInfo stockStructureProfileInfo = null;
			List<EbaseInfo> ebaseInfoList = shareholderMapper.queryEbaseInfoByEntname(requestEntnameVal);
			if(ebaseInfoList!=null && ebaseInfoList.size()>0){
				for(EbaseInfo ebaseInfo : ebaseInfoList){
					stockStructureProfileInfo = JSON.parseObject(resultJson, StockStructureProfileInfo.class);
					
					if(!StringUtils.isNullOrEmpty(unique)){
						stockStructureProfileInfo.setUniqueId(unique);
					}
					stockStructureProfileInfo.setPripid(ebaseInfo.getPripid());
					//企业名称
					stockStructureProfileInfo.setEntname(entname);
					//地市Id
					stockStructureProfileInfo.setCityId(ebaseInfo.getCityId());
					System.out.println("股权结构图非循环体{{}}==>json转换对象数据{{}}==>" + stockStructureProfileInfo);
					//保存股权结构图
					shareholderMapper.saveStockStructureProfileInfo(stockStructureProfileInfo);
				}
			}else{
				stockStructureProfileInfo = JSON.parseObject(resultJson, StockStructureProfileInfo.class);
				
				if(!StringUtils.isNullOrEmpty(unique)){
					stockStructureProfileInfo.setUniqueId(unique);
				}
				//企业名称
				stockStructureProfileInfo.setEntname(entname);
				System.out.println("股权结构图非循环体{{}}==>json转换对象数据{{}}==>" + stockStructureProfileInfo);
				//保存股权结构
				shareholderMapper.saveStockStructureProfileInfo(stockStructureProfileInfo);
			}
			
			if(resultJsonObject.containsKey("Children") && resultJsonObject.get("Children")!=null){
				JSONArray array = resultJsonObject.getJSONArray("Children");
				for (int i = 0; i < array.size(); i++) {
					JSONObject arrJsonObject = array.getJSONObject(i);
					String arrJsonStr = arrJsonObject.toJSONString();
					stockStructureProfileInfo = JSON.parseObject(arrJsonStr, StockStructureProfileInfo.class);
					if(!StringUtils.isNullOrEmpty(unique)){
						stockStructureProfileInfo.setUniqueId(unique);
					}
					//企业名称
					stockStructureProfileInfo.setEntname(entname);
					System.out.println("股权结构图循环体{{}}==>json转换对象数据{{}}==>" + stockStructureProfileInfo);
					//保存股权结构
					shareholderMapper.saveStockStructureProfileInfo(stockStructureProfileInfo);
				    if(arrJsonObject.containsKey("Children") && (arrJsonObject.get("Children")!=null || !"null".equals(arrJsonObject.get("Children").toString()))){
				    	//解析子json数据
				    	saveStockStructureProfileChildrenData(arrJsonObject.get("Children").toString(),unique,entname);
				    }
				}
			}
		} catch (Exception e) {
			System.err.println("io.github.biezhi.elves.service.impl.StockStructureServiceImpl.saveStockStructureProfileData，解析json数据并保存股权结构图数据入库异常，cmsAsynUrl链接为:" + cmsAsynUrl + "，json串为:" + jsonStr + "，异常信息{{}}==>" + StringUtils.getErrorInfoFromException(e));
			
			System.out.println("io.github.biezhi.elves.service.impl.StockStructureServiceImpl.saveStockStructureProfileData，休眠5000毫秒后，重新再调用......");
			try {
				Thread.sleep(5000);
				System.out.println("io.github.biezhi.elves.service.impl.StockStructureServiceImpl.saveStockStructureProfileData，已休眠5000毫秒，开始重新再调用saveStockStructureProfileData方法===>");
				System.err.println("企业名称{{}}==>" + entname);
				//返回响应url内容
				jsonStr = HttpUtil.getQccGetHttpReponseBody(cmsAsynUrl);
				saveStockStructureProfileData(cmsAsynUrl,jsonStr, unique, entname, requestEntnameVal);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	/**
	 * 解析股权结构图子json数据
	 * @param jsonStr
	 */
	private void saveStockStructureProfileChildrenData(String jsonStr,String unique,String entname){
		try{
			JSONArray array = JSONArray.parseArray(jsonStr);
			if(array!=null){
				for (int i = 0; i < array.size(); i++) {
					JSONObject arrJsonObject = array.getJSONObject(i);
					String arrJsonStr = arrJsonObject.toJSONString();
					StockStructureProfileInfo stockStructureProfileInfo = JSON.parseObject(arrJsonStr, StockStructureProfileInfo.class);
					if(!StringUtils.isNullOrEmpty(unique)){
						stockStructureProfileInfo.setUniqueId(unique);
					}
					//企业名称
					stockStructureProfileInfo.setEntname(entname);
					System.out.println("股权结构图子循环体{{}}==>json转换对象数据{{}}==>" + stockStructureProfileInfo);
					try {
						//保存股权结构图
						shareholderMapper.saveStockStructureProfileInfo(stockStructureProfileInfo);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}catch(Exception e){
			System.err.println("io.github.biezhi.elves.service.impl.StockStructureServiceImpl.saveStockStructureProfileChildrenData，解析子json数据并保存股权结构图数据入库异常，json串为:" + jsonStr + "{{}}==>" + StringUtils.getErrorInfoFromException(e));
			
			System.out.println("io.github.biezhi.elves.service.impl.StockStructureServiceImpl.saveStockStructureProfileChildrenData，休眠5000毫秒后，重新再调用......");
			try {
				Thread.sleep(5000);
				
				System.out.println("io.github.biezhi.elves.service.impl.StockStructureServiceImpl.saveStockStructureProfileChildrenData，已休眠5000毫秒，开始重新再调用saveStockStructureProfileChildrenData方法===>");
				saveStockStructureProfileChildrenData(jsonStr, unique, entname);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
		}
	}
}